﻿using System;
using System.Collections.Generic;
using Dorm.QueryObject;
using Dorm.Testes;

namespace Dorm.Tests
{
    public class InsertDeleteTests
    {
        public static void GenerateData()
        {
            var db = new Database();

            try
            {
                db.BeginTransaction();

                var type1 = new UserType {Name = "Internal"};
                var type2 = new UserType {Name = "External"};
                
                var fakeUserTypes = new List<UserType> { type1, type2 };

                // Test Simple Save
                foreach (var userType in fakeUserTypes)
                    db.Save(userType);

                db.CommitTransaction();
                db.BeginTransaction();

                var fakeUsers = new List<Person>
                {
                    new Person { Name = "User 1", Type = type1, HomeAddress = new Address { Street = "A", City = "B", Number = 10} },
                    new Person { Name = "User 2", Type = type2, HomeAddress = new Address { Street = "A", City = "B", Number = 10} },
                    new Person { Name = "User 3", Type = type1, HomeAddress = new Address { Street = "A", City = "B", Number = 10} },
                    new Person { Name = "User 4", Type = type2, HomeAddress = new Address { Street = "A", City = "B", Number = 10} },
                    new Person { Name = "User 5", Type = type1, HomeAddress = new Address { Street = "A", City = "B", Number = 10} },
                    new Person { Name = "User 6", Type = type2, HomeAddress = new Address { Street = "A", City = "B", Number = 10} },
                    new Person { Name = "User 7", Type = type1, HomeAddress = new Address { Street = "A", City = "B", Number = 10} },
                    new Person { Name = "User 8", Type = type2, HomeAddress = new Address { Street = "A", City = "B", Number = 10} },
                    new Person { Name = "User 9", Type = type1, HomeAddress = new Address { Street = "A", City = "B", Number = 10} },
                    new Person { Name = "User 10", Type = type1, HomeAddress = new Address { Street = "A", City = "B", Number = 10} },
                    new Person { Name = "User 11", Type = type2, HomeAddress = new Address { Street = "A", City = "B", Number = 10} },
                };

                fakeUsers[0].Permissions = new List<Permission>
                {
                    new Permission {ScreenName = "Screen 1"},
                    new Permission {ScreenName = "Screen 2"},
                    new Permission {ScreenName = "Screen 3"},
                };

                fakeUsers[2].Permissions = new List<Permission>
                {
                    new Permission {ScreenName = "Screen 1"},
                    new Permission {ScreenName = "Screen 3"},
                };

                fakeUsers[4].Permissions = new List<Permission>
                {
                    new Permission {ScreenName = "Screen 2"},
                };

                // Test Save with Has Many
                foreach (var user in fakeUsers)
                    db.Save(user);

                db.CommitTransaction();
                db.BeginTransaction();

                var fakeProfiles = new List<Profile>
                {
                    new Profile { Name = "Admin" },
                    new Profile { Name = "Common User" }
                };

                fakeProfiles[0].Permissions = new List<Permission>
                {
                    new Permission {ScreenName = "Screen 1"},
                    new Permission {ScreenName = "Screen 2"},
                    new Permission {ScreenName = "Screen 3"},
                    new Permission {ScreenName = "Screen 4"},
                };

                fakeProfiles[1].Permissions = new List<Permission>
                {
                    new Permission {ScreenName = "Screen 1"},
                    new Permission {ScreenName = "Screen 2"},
                    new Permission {ScreenName = "Screen 3"},
                    new Permission {ScreenName = "Screen 4"},
                };

                // Test Save with Has Many to Many
                fakeProfiles[0].Users = new List<Person> { fakeUsers[0] };
                fakeProfiles[1].Users = new List<Person> { fakeUsers[1], fakeUsers[2] };

                foreach (var profile in fakeProfiles)
                    db.Save(profile);

                db.CommitTransaction();
            }
            catch (Exception)
            {
                db.RollbackTransaction();
                throw;
            }

            db.Dispose();
        }

        public static void CleanData()
        {
            var db = new Database();

            try
            {
                db.BeginTransaction();

                db.DeleteAll(new Query<Person>());
                db.DeleteAll(new Query<Profile>());
                db.DeleteAll(new Query<UserType>());

                db.CommitTransaction();
            }
            catch (Exception)
            {
                db.RollbackTransaction();
                throw;
            }

            db.Dispose();
        }
    }
}
